@echo off
setlocal EnableDelayedExpansion

DEL /q "%CD%\target"
SET "CONTAINER_NAME=aimis-app"
SET "IMAGE_NAME=aimis-image"
SET "TAG=latest"
SET "ENV=dev"

FOR /F "tokens=1" %%i in ('docker ps -a ^| findstr "%CONTAINER_NAME%"') do @set container=%%i
FOR /F "tokens=3" %%j in ('docker image ls %IMAGE_NAME%:%TAG% ^| findstr "%IMAGE_NAME%"') do @set image=%%j

IF "%container%" neq "" (docker rm -f %container% && echo "delete container")
IF "%image%" neq "" (docker rmi %image%  && echo "delete image")

docker run --rm -v %CD%:/app --name maven maven:3.8.7-eclipse-temurin-17-focal /bin/bash -c "cp /app/settings.xml /usr/share/maven/conf && cd /app && mvn clean package -P%ENV% -Dmaven.test.skip=true"
docker build -f Dockerfile -t %IMAGE_NAME%:%TAG% . && docker run  --pid=host --privileged=true -e spring.profiles.active=dev -d -p 514:514/udp -p 9000:9000 -v ~/aimis/uploadFile:/uploadFile --restart always --name %CONTAINER_NAME% %IMAGE_NAME%:%TAG%